<?php

session_start();
/*
  de publish album ta can lam nhung cong viec sau
 * lay ve access_token cua page
 * publish album
 * ghi id cua album tra ve vao file jsons
 * upload photo vao dia chi album thong qua id trong file json
 * ghi bang tham chieu facebook_song (cac gia tri la id cua facebook va id cua bai hat)
 */

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( './FacebookAction/FacebookAlbum.php' );
require_once( './FacebookAction/FacebookAccesstoken.php' );
require_once( './FacebookAction/FacebookPhoto.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;

include 'config.php';
FacebookSession::setDefaultApplication($id, $secret);

$helper = new FacebookRedirectLoginHelper('http://localhost/itv-facebook-app/publishalbum.php');

try {
    $session = $helper->getSessionFromRedirect();
} catch (Exception $e) {
    
}

if (isset($_SESSION['token'])) {

    $session = new FacebookSession($_SESSION['token']);

    try {

        $session->Validate($id, $secret);
    } catch (FacebookAuthorizationException $e) {

        $session = '';
    }
}

$a = new \FacebookAction\FacebookAccesstoken();


if (isset($session)) {
    set_time_limit(0);
    $_SESSION['token'] = $session->getToken();

    //----lay ve access_token cua page
    $fa = new FacebookAction\FacebookAccesstoken();
    $access_token_itv = $fa->getAccesstokenFanpage($session, $itv_id);
    //----publish album
    $fab = new FacebookAction\FacebookAlbum();
    $album_name = 'Nhạc việt nam';
    $message = 'Các bạn nhanh tay nhấn like để vote cho bài hát mình yêu thích nhé!';
    $json_album_recieve = $fab->publishAlbumToFanpage($session, $itv_id, $access_token_itv['access_token'], $album_name, $message);
    //-----ghi id cua album tra ve vao file json
    $json_album_id = json_encode($json_album_recieve);
    file_put_contents('album_id.json', $json_album_id);
    //---- upload photo vao dia chi album vua ghi
    $fp = new \FacebookAction\FacebookPhoto();
    $result = mysqli_query($con, "SELECT * FROM song WHERE album_id='A01'");
    while ($row = mysqli_fetch_array($result)) {
        $message_song = "Mã số bài hát' " . $row['song_id'] . "  -  Ca sĩ' " . $row['singer'] . "   -  Ca khúc: " . $row['song_name'];
        $file_upload = new CURLFile($row['song_picture'], 'image/jpg');
        $fb_song_id = $fp->publishPhotoToAlbum($session, $json_album_recieve['id'], $access_token_itv['access_token'], $file_upload, $message_song);
        //------- Ghi bảng tham chiếu facebook_song
        $sql = "INSERT INTO facebook_song (facebook_id, song_id) VALUES ('" . $fb_song_id['id'] . "', '" . $row['song_id'] . "')";
    }
    mysqli_close($con);
} else {
    echo "<a href = " . $helper->getLoginUrl() . ">Login With Facebook</a>";
}










